package com.webside.points.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.ExpertEntity;
import com.webside.points.model.IssueTypeEntity;
import com.webside.points.model.UserEntity;
import com.webside.points.service.ExpertService;
import com.webside.points.service.IssueTypeService;
import com.webside.points.service.UserService;

@Controller
@Scope("prototype")
@RequestMapping("/issueType/")
public class IssueTypeController {
	
	@Autowired
	IssueTypeService issueTypeService;
	
	@Autowired
	ExpertService expertService;
	
	@Autowired
	UserService userService;
	
	
	@RequestMapping("findAll")
	public String findAll(Model model,String mark,String openId){
		model.addAttribute("issueTypeList", issueTypeService.findAll());
		model.addAttribute("openId", openId);
		
		UserEntity userEntity = userService.findByOpenId(openId);
		model.addAttribute("user", userEntity);
		if(mark==null){
			return "serve/issuetype";
		}else if(mark.equals("expert")){
			return "service/expert";
		}
		return "service/issue";
	}
	
	@RequestMapping("findAllExpert")
	public void findAllExpert(int page,int size,HttpServletResponse response){
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		if(page!=0){
			 int cPage=page;
			 cPage=(cPage-1)*10;
			 map.put("page", cPage);
			 map.put("size", size);
		 }
		System.out.println("map:>>>"+map);
		List<ExpertEntity> list = expertService.finaAllExpert(map);
		
		System.out.println(list);
		String json = JSON.toJSONString(list);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
		
	}
	
	@RequestMapping("insert")
	public String insert(IssueTypeEntity issueTypeEntity){
	
		
		issueTypeEntity.setCreateTime(new Date(System.currentTimeMillis()));
		issueTypeEntity.setStatus("1");
		
		System.out.println(issueTypeService.insert(issueTypeEntity));
		return "redirect:findAll";
	}

	@RequestMapping("delete")
	public String delete(int id){
		
		System.out.println(issueTypeService.delete(id));
		return "redirect:findAll";
	}

	@RequestMapping("update")
	public String update(IssueTypeEntity issueTypeEntity){
		
		issueTypeEntity.setCreateTime(new Date(System.currentTimeMillis()));
		
		System.out.println(issueTypeService.update(issueTypeEntity));
		return "redirect:findAll";
	}
	
	@RequestMapping("findById")
	public String findById(int id,Model model){
		
		model.addAttribute("issueType",issueTypeService.findById(id));
		return "serve/issueTypeForm";
	}
	
	@RequestMapping("updateStatus")
	public String updateStatus(IssueTypeEntity issueTypeEntity){
		
		System.out.println("进入修改状态：>>>"+issueTypeEntity);
		System.out.println(issueTypeService.updateStatus(issueTypeEntity));
		return "redirect:findAll";
	}
	
	@RequestMapping("findByIssueTypeId")
	public void findByIssueTypeId(int id,Model model,HttpServletResponse response){
		
		IssueTypeEntity issueTypeEntity = issueTypeService.findById(id);
		String json = JSON.toJSONString(issueTypeEntity);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
	}
	@RequestMapping("findIssueType")
	public void findIssueType(HttpServletResponse response){
		List<IssueTypeEntity> list = issueTypeService.findAll();
		String json = JSON.toJSONString(list);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
	}
	

}
